<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText" :titleExplain="titleExplain"></topTitle>
    <div class="mainCon">
      <el-row class="searchBox searchInputWidth">
        <el-form :inline="true" :model="filterField" ref="accountForm">
          <el-col :lg="8" :xl="6">
            <el-form-item label="IP地址" class="row-padding-bottom">
              <el-input
                placeholder="请输入IP地址"
                clearable
                v-model="filterField.ip"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="状态" class="row-padding-bottom">
              <el-select
                v-model="filterField.status"
                placeholder="请选择"
                clearable
              >
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-form-item>
            <!-- <el-button
                            type="primary"
                            icon="icon iconfont iconchazhao1"
                            @click="searchList"
                            class="commonBtn"
                        ></el-button> -->
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
            <!-- <exportBtn :dataLen="tableData.length" :filterField="filterField" api="api-s/report/export_SYS_WHITE_IP"></exportBtn> -->
            <!-- <el-button
                            @click="addIpAddress"
                            class="searchBtn saveBtn"
                            >添加</el-button
                        > -->
            <!-- <el-button :disabled="isDisabled" class="commonBut saveBtn funComBut addSecondTab" icon="icon iconfont icontianjia" @click = "addIpAddress" >
                            <span class="opetareText">添加</span>  
                        </el-button> -->
          </el-form-item>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px">
          <exportBtn
            :dataLen="tableData.length"
            :filterField="filterField"
            api="api-s/report/export_SYS_WHITE_IP"
          ></exportBtn>
          <el-button
            :disabled="isDisabled"
            class="commonBut saveBtn funComBut addSecondTab"
            icon="icon iconfont icontianjia"
            @click="addIpAddress"
          >
            <span class="opetareText">添加</span>
          </el-button>
        </div>
        <!-- 列表 -->
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableData"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          v-loading="loading"
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            align="center"
            prop="ip"
            label="IP地址"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column align="center" label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.status === 0 || scope.row.status === 1">
                {{ scope.row.status === 0 ? '禁用' : '启用' }}
              </span>
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="remark"
            label="备注"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column align="left" label="操作" fixed="right" width="180">
            <template slot-scope="scope">
              <el-link
                v-if="isShowUpdate"
                type="primary"
                @click="updateStatus(scope.row)"
                >{{ scope.row.status === 0 ? '启用' : '禁用' }}</el-link
              >
              <el-link
                v-if="isShowUpdate"
                type="primary"
                @click="editStatus(scope.row)"
                >修改</el-link
              >
              <el-link
                v-if="isShowUpdate"
                type="primary"
                @click="delConfirm(scope.row.id)"
                >删除</el-link
              >
              <span v-if="!isShowUpdate">--</span>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
        ></Pagination>
      </div>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="delDialogVisible"
      width="30%"
      center
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <span>请确认是否删除该条信息？</span>
      <span slot="footer" class="dialog-footer">
        <el-button class="btn cancel" @click="delDialogVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" class="btn save" @click="delIpAddress()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <WhiteListDialog
      v-if="dialogVisible"
      :dialogVisible="dialogVisible"
      :whiteListDetail="whiteListDetail"
      :dialogType="dialogType"
      @closeDialog="closeDialog"
    ></WhiteListDialog>
  </div>
</template>

<script>
import WhiteListDialog from './WhiteListDialog'
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'WhiteList',
  mixins: [scrollTable],
  components: {
    WhiteListDialog,
  },
  data() {
    return {
      titleText: 'IP白名单管理',
      titleExplain:
        '纳入白名单的ip地址访问系统可跳过异地登录校验，且不影响用户使用。',
      requestUrl: 'api-m/whiteIPList',
      filterField: {
        ip: '',
        status: '',
      },
      id: '',
      delDialogVisible: false,
      pageSize: 20,
      pageNo: 1,
      tableData: [], //表格数据
      loading: true,
      isShow: false,
      dialogVisible: false,
      whiteListDetail: {},
      dialogType: '',
      isShowUpdate: true,
    }
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    // this.isShowUpdate =
    //     this.$store.state.buttonLimitsArr.indexOf("mCompanyAudit:detail") >
    //     -1;
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.tableData = data
      this.loading = false
      if (this.tableData.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    closeDialog(val) {
      this.dialogVisible = false
      if (val === 'update') {
        this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      }
    },
    // 添加
    addIpAddress() {
      this.dialogType = 'add'
      this.dialogVisible = true
    },
    // 修改
    editStatus(row) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.whiteListDetail = row
    },
    // 启用/禁用
    updateStatus({ id, status, remark, ip } = row) {
      return this.axios({
        method: 'put',
        url: 'api-m/whiteIP',
        data: {
          id,
          remark,
          ip,
          status: status === 0 ? 1 : 0,
        },
        headers: {
          'Content-Type': 'application/json',
        },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.$message.success('设置成功')
            this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 二次确认
    delConfirm(id) {
      this.id = id
      this.delDialogVisible = true
    },
    // 删除
    delIpAddress() {
      return this.axios({
        method: 'delete',
        url: 'api-m/whiteIP?id=' + this.id,
        // params: {
        //     id,
        // }
      })
        .then((response) => {
          this.delDialogVisible = false
          if (!response.data.hasError) {
            this.$message.success('删除成功')
            this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          this.delDialogVisible = false
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
  },
}
</script>

<style scoped lang="less">
@import '../../../static/css/filterArea.less';
.mainCon {
  top: 213px;
}
</style>
